package Main;

import SemanticSpace.words.tools.WordIdSuper;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import tools.Derby.DerbyManager;
import SemanticSpace.words.tools.WordTool;
import java.awt.ItemSelectable;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class Main extends DerbyManager {

    private JFrame jf;
    private JTextField jtf;
    private JComboBox jcb;
    private JList list;
    private String word;
    private JScrollPane scroll;
    HashMap<Integer, String> idWord;
    HashMap<String, Integer> wordId;
    HashMap<Integer, String> idDist;
    JPanel jp2 = new JPanel();
    HashMap<Integer, HashMap<Integer, Integer>> idVector;

    public Main() throws SQLException {
        super();
        ResultSet rs = stmt.executeQuery("select id, word,dist,vector from words");
        idVector = new WordIdSuper(rs, 0).getHashMap();//id,vector
        idWord = new WordIdSuper(rs, 1).getHashMap();//id,word
        wordId = new WordIdSuper(rs, 4).getHashMap();//word,id
        idDist = new WordIdSuper(rs, 5).getHashMap();//word,id
        jf = new JFrame("Relative words retriver.");
        jf.setLayout(new GridLayout(1, 3));
        JLabel l1 = new JLabel("词典表中词汇");
        JLabel l2 = new JLabel("词典表中的相关词汇");
        JLabel l3 = new JLabel("语义距离，最小为0，最大为根号2");
        JPanel jp1 = new JPanel();

        JPanel jp3 = new JPanel();
        jf.add(jp1);
        jf.add(jp2);
        jf.add(jp3);

        jcb = new JComboBox(idWord.values().toArray());
        jp1.add(l1);
        jp1.add(jcb);
        list = new JList();
        list.setAlignmentX(1000);
        list.setSize(500, 500);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        scroll = new JScrollPane(list);
        // scroll.setBounds(60, 15, 80, 180);
        jp2.add(l2);
        jp2.add(scroll);
        jtf = new JTextField(10);
        jp3.add(l3);
        jp3.add(jtf);
        this.addEventHandler();
        jf.setSize(700, 300);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    /**
     * 添加事件
     */
    private void addEventHandler() {
        //JCombox事件监听
        jcb.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Object selected[] = ((ItemSelectable) e.getSource()).getSelectedObjects();
                word=((selected.length == 0) ? "null" : (String) selected[0]);
                if (null!=word&&null != idDist.get(wordId.get(word))) {
                    String[] tem = idDist.get(wordId.get(word)).split(",");
                    String[] word = new String[tem.length];
                    for (int i = 0; i < tem.length; i++) {
                        word[i] = idWord.get(Integer.parseInt(tem[i]));
                    }
                    if (tem != null) {
                        list.setListData(word);
                    }
                    list.setSelectedIndex(0);
                    list.validate();
                    jp2.validate();
                } else {
                    String title = "提示信息";
                    int optionType = JOptionPane.DEFAULT_OPTION;
                    int messageType = JOptionPane.WARNING_MESSAGE;
                    list.setListData(new Vector());
                    JOptionPane.showConfirmDialog(jf, "没有关联词汇!! ", title, optionType, messageType);//t是弹出窗口的基层窗口
                }
            }
        });
        //JList事件监听
        list.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                jtf.setText("");
                //System.out.println("123" + e.getSource());
                String s1 = (String) ((JList) e.getSource()).getSelectedValue();
                try {
                    WordTool dc = new WordTool();
                    float f = dc.getDistance(idVector.get(wordId.get(word)), idVector.get(wordId.get(s1)));
                    jtf.setText(f + "");
                } catch (Exception e1) {
                }
            }
        });
    }

    public static void main(String[] args) throws SQLException {
        Main m = new Main();
        m.destroy();
    }
}
